import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:yt_devops/components/network_image_load/load_image.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/styles/styles.dart';

class pwdTextField extends StatefulWidget {

  final TextEditingController textFieldController;
  final String hintString; //占位文字
  final bool isShowCleanTextBtn; //是否显示清除文字按钮?
  final bool isShowPwdEye; //是否显示密码的眼睛?
  final bool isHiddenPwd; //是否明文显示
  final TextInputType textInputType; //输入键盘类型
  final int textLength; //显示的字数
  final int maxLines; //是否多行显示
  final double fontSize;//字体大小
  final bool isLeft; //文字是否靠左显示
  final Function(String) callBack;

  const pwdTextField(
      this.textFieldController, this.hintString,
      this.callBack,
      {this.isShowCleanTextBtn = false,
        this.isShowPwdEye = true,
        this.textInputType = TextInputType.emailAddress,
        this.maxLines = 1,
        this.textLength = 40,
        this.isHiddenPwd = true,
        this.fontSize = 16,
        this.isLeft = false,
        Key? key})
      : super(key: key);
  @override
  State<pwdTextField> createState() => _pwdTextFieldState();
}

class _pwdTextFieldState extends State<pwdTextField> {
  bool _isHiddenPwd = true;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _isHiddenPwd = widget.isHiddenPwd;

  }
  @override
  Widget build(BuildContext context) {
    List<TextInputFormatter> ?inputFormatters = [
      LengthLimitingTextInputFormatter(widget.textLength)
    ];
    return Container(
      // color: Colours.redE44545Color,
      height: 50,
      // margin: const EdgeInsets.only(left: 16,right: 16),
      alignment: Alignment.centerRight,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(5),
          color: Colors.white,
          // border: Border.all(color: Colours.textBlack101B12Color,width: 0.5)
      ),
      child: Row(
        children: [
          Expanded(
            child: TextField(
              maxLines: widget.maxLines,
              textAlign: widget.isLeft?TextAlign.left:TextAlign.right,
              inputFormatters: inputFormatters,
              style:  ytTextStyle(fontSize: widget.fontSize,),
              controller: widget.textFieldController,
              keyboardType: widget.textInputType,
              obscureText: _isHiddenPwd,
              decoration: InputDecoration(
                border: InputBorder.none,
                contentPadding: const EdgeInsets.all(14.0),
                hintText: widget.hintString,
                hintStyle:  TextStyle(
                    fontSize: widget.fontSize,
                    color: Colour.hintTextColor,
                    fontWeight: FontWeight.normal),
              ),
              onChanged: (String str) {
                widget.callBack!(str);
              },
            ),
          ),
          Visibility(
              visible: widget.isShowPwdEye,
              child: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  _isHiddenPwd = !_isHiddenPwd;
                  setState(() {});
                },
                child: Container(
                  padding: const EdgeInsets.only(right: 20,),
                  child: LoadAssetImage(
                      !_isHiddenPwd
                          ? 'assets/images/icon_pw_show'
                          : 'assets/images/icon_pw_hide',
                      height: 16,
                      width: 16,
                      // color: Colours.whiteColor,
                      fit: BoxFit.cover),
                ),
              )),
        ],
      ),
    );
  }
}

